﻿using System;
using System.Collections;
using System.Collections.Generic;
using BillBook.Model;
using BillBook.Repository;
using NHibernate;
using NHibernate.Criterion;

namespace BillBook.Controller
{
    public class UserController
    {
        private readonly IBillBookRepository repository;

        public UserController():this(new BillBookRepository())
        {
        }

        public UserController(IBillBookRepository repository)
        {
            this.repository = repository;
        }

        public sbyte Save(User user)
        {
            try
            {
            repository.Save(user);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
            
        }
        public sbyte Delete(User user)
        {
            try
            {
                repository.Delete(user);
                return 1;
            }
            catch(Exception)
            {
                return 0;
            }
            
        }
        public sbyte Update(User user)
        {
            try
            {
                repository.Update(user);
                return 1;
            }
            catch(Exception)
            {
                return 0;
            }
        }

        public User Authenticate(string username,string password)
        {
            try
            {
                List<User> users = repository.ExecuteNamedQuery<User>("AuthenticateUser", new Dictionary<string, object> { { "userName", username }, { "password", password } ,{"status",1}});
                return users[0];
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public sbyte ChangePassword(long userid,string password)
        {
            User user=repository.Load<User>(userid);
            user.UserID = userid;
            user.Password = password;
            return this.Update(user);
        }

        public sbyte ChangeUserDetails(long userid, string firstname, string lastname, string email)
        {
            User user = repository.Load<User>(userid);
            user.UserID = userid;
            user.FirstName = firstname;
            user.LastName = lastname;
            user.Email = email;
            return this.Update(user);
        }

        public List<User> ListUsers(int startrecordno,int numofrecords)
        {
            try
            {
                List<User> users = repository.ExecuteNamedQueryList<User>("ListUsers", new Dictionary<string, object>(),startrecordno,numofrecords);
                return users;
            }
            catch (Exception e)
            {
                return null;
            } 
        }

        public int Count()
        {
            try
            {
                object count = repository.ExecuteScalar<User>("UserCount", new Dictionary<string, object>());
                return Convert.ToInt32(count);
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public User GetUser(long userid)
        {
            return repository.Load<User>(userid);
        }
    }
}
